<?php
/**
 * Created by PhpStorm.
 * User: z-benjamin
 * Date: 16/9/9
 * Time: 上午11:18.
 */

namespace wxml\controllers;

use wxml\models\Weixin;
use yii;
use common\libs\CommonController;
use wxml\models\user\User;

class UserController extends CommonController
{
    public function actionSession_key()
    {
        $weixin = new Weixin();
        $code = Yii::$app->request->post('code');
        $result = $weixin->login($code);
        if ($result === false) {
            return $this->ajaxResponseError($result['errmsg']);
        } else {
            return $this->ajaxResponseSuccess(['third_session' => $result]);
        }
    }

    public function actionUpdate_user_info()
    {
        $userInfo = yii::$app->request->post('user_info');
        $accessToken = yii::$app->request->post('access_token');
        $user = Weixin::checkIsLogin($accessToken);
        if ($user === false) {
            return $this->ajaxResponseError('用户还未登录，请重新登录', -999);
        }
        $user->nickname = $userInfo['nickName'];
        $user->name = $userInfo['nickName'];
        $user->update_time = time();
        $user->sex = $userInfo['gender'];
        $user->avatar = $userInfo['avatarUrl'];
        $result = $user->save();
        if ($result) {
            return $this->ajaxResponseSuccess();
        } else {
            return $this->ajaxResponseError('更新用户信息失败', -1);
        }
    }
}
